import Vue from 'vue'
import Router from 'vue-router'
import loginView from '../views/login.vue'
//import Template from'../components/template.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login1',
      component: loginView,
    },
    {
      path: '/login',
      name: 'login2',
      component: loginView,
    },
    {
      path: '/index',
      name: 'index',
      component: () =>
        import(/* webpackChunkName: "about" */ '../views/index.vue'),
      //redirect:'/template',
      children: [
        //{path: '/template',component:Template},
      ],
      meta: {
        keepAlive: true,
      },
    },
    {
      path: '/info',
      name: 'info',
      component: () => import('../components/info.vue'),
    },
  ],
})
//挂在路由导航卫士
router.beforeEach((to, form, next) => {
  //console.log(to)
  // form 从哪里来; to 到哪里去; next() 一个函数 next()放行, next('/home)强制跳转
  if (to.path === '/login') {
    return next()
  }
  //验证token
  const tokenstr = window.sessionStorage.getItem('token')
  //const tokenstr = localStorage.getItem('token')
  if (!tokenstr) {
    return next('/login')
  }
  next()
})

export default router
